package com.bubblegumproject.ogo.swing.events;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 * Condition triggered when the user presses a key.
 *
 * @author Azubuko Obele (buko.obele@gmail.com)
 */
public class KeyPressedCondition extends AbstractCondition {

    public KeyPressedCondition(JComponent component, KeyStroke keystroke) {
        assert(component != null && keystroke != null);
        component.getInputMap().put(keystroke, keystroke.toString());
        component.getActionMap().put(keystroke.toString(), new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                occured();
            }
        });
    }

    public KeyPressedCondition(RootPaneContainer container, KeyStroke keyStroke) {
        assert(container != null && keyStroke != null);
        container.getRootPane().getInputMap().put(keyStroke, keyStroke.toString());
        container.getRootPane().getActionMap().put(keyStroke.toString(), new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                occured();
            }
        });
    }

    public static KeyPressedCondition KeyPressed(JComponent component, KeyStroke keyStroke) {
        return new KeyPressedCondition(component, keyStroke);
    }

    public static KeyPressedCondition KeyPressed(RootPaneContainer container, KeyStroke keyStroke) {
        return new KeyPressedCondition(container, keyStroke);
    }
}
